package com.ftg.learn.bus.sharecarexp_record.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.bus.sharecarexp_record.entity.SharecarExpRecord;
import com.ftg.learn.bus.sharecarexp_record.mapper.SharecarExpRecordMapper;
import com.ftg.learn.bus.util.ResultType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SharecarExpRecordService {

    @Autowired
    SharecarExpRecordMapper scer;

    /**
     * 查询
     * @return
     */
    public List<SharecarExpRecord> showAll() {
        return scer.showAll();
    }

    /**
     * 删除单条
     * @param id
     * @return
     */
    public int delete(String id) {
        if(id == null || id.equals("")){
            return -1;
        }else{
            return scer.deleteById(id);
        }
    }

    /**
     * 查一条记录
     * @param id
     * @return
     */
    public SharecarExpRecord findOne(String id) {
        return scer.selectById(id);
    }

    /**
     * 基础操作添加
     * @return
     */
    public int SharecarExpInsertRecord(String id,String name){
        String eid = "在公车费用表中添加一条数据,相关车牌号:"+id;
        return scer.autoInsert(eid,name);
    }

    /**
     * 基础操作修改
     * @return
     */
    public int SharecarExpUpdateRecord(String id,String name){
        String eid = "在公车费用表中修改一条数据,相关车牌号:"+id;
        return scer.autoInsert(eid,name);
    }

    /**
     * 分页查询
     * @param page                     总页数
     * @param limit                    每页显示条数
     * @param sharecar_expend_id          操作id
     * @param sharecar_expend_operater    操作人
     * @return
     */
    public ResultType<SharecarExpRecord> query(int page, int limit, String sharecar_expend_id, String sharecar_expend_operater){

        ResultType<SharecarExpRecord> rt = new ResultType<>();
        rt.setCode(0);

        IPage<SharecarExpRecord> ip = new Page<>(page,limit);

        QueryWrapper<SharecarExpRecord> qw = new QueryWrapper<>();
        qw.and(wrapper -> wrapper.like("sharecar_expend_id",sharecar_expend_id).like("sharecar_expend_operater",sharecar_expend_operater));

        ip =  scer.selectPage(ip,qw);
        rt.setData(ip.getRecords());
        rt.setCount(ip.getTotal());
        return rt;
    }
}
